package me.huanmeng.heater.sender.manager;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import me.huanmeng.heater.Heater;
import me.huanmeng.heater.exception.ResponseException;
import me.huanmeng.heater.http.KaiheilaApi;
import me.huanmeng.heater.main.HeaterMain;
import me.huanmeng.heater.object.Guild;
import me.huanmeng.heater.object.UserInfo;
import me.huanmeng.heater.sender.User;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2021/10/4<br>
 * Heater<br>
 *
 * @author huanmeng_qwq
 */
public class UserManager {
    private final Map<String, UserInfo> userInfos;
    private final Map<String, User> users;

    public UserManager() {
        this.userInfos = new HashMap<>();
        this.users = new HashMap<>();
        init();
    }

    void init() {
        for (Guild guild : Heater.getGuilds()) {
            for (UserInfo userInfo : getUsersHttp(guild, 1)) {
                updateUserInfo(userInfo);
            }
        }
    }

    List<UserInfo> getUsersHttp(Guild guild, int page) {
        List<UserInfo> userInfolist = Lists.newArrayList();
        final KaiheilaApi kaiheilaApi = HeaterMain.getInstance().getKaiheilaApi();
        final String body = kaiheilaApi.doGet("/guild/user-list")
                .form("guild_id", guild.getId())
                .form("page", page)
                .form("page_size", 30)
                .executeAsync().body();
        final JSONObject jsonObject = new JSONObject(body);
        if (jsonObject.getInt("code", -1) != 0) {
            throw new ResponseException(body);
        }
        for (Object o : jsonObject.getJSONObject("data").getJSONArray("items")) {
            userInfolist.add(JSONUtil.toBean((JSONObject) o, UserInfo.class));
        }
        if (jsonObject.getJSONObject("data").getJSONObject("meta").getInt("page_total", page) > page) {
            userInfolist.addAll(getUsersHttp(guild, page + 1));
        }
        return userInfolist;
    }

    @Nullable
    public User getUser(String id) {
        return users.get(id);
    }

    @Nullable
    public UserInfo getUserInfo(String id) {
        return userInfos.get(id);
    }

    public void updateUserInfo(UserInfo userInfo) {
        if (userInfos.containsKey(userInfo.getId())) {
            final UserInfo u = userInfos.get(userInfo.getId());
            u.setOs(userInfo.getOs());
            u.set_vip(userInfo.is_vip());
            u.setBot(userInfo.isBot());
            u.setRoles(userInfo.getRoles());
            u.setVip_avatar(userInfo.getVip_avatar());
            u.setAvatar(userInfo.getAvatar());
            u.setNickname(userInfo.getNickname());
            u.setOnline(userInfo.isOnline());
            u.setId(userInfo.getId());
            u.setIdentify_num(userInfo.getIdentify_num());
            u.setUsername(userInfo.getUsername());
            u.setStatus(userInfo.getStatus());
        } else {
            users.put(userInfo.getId(), new User(userInfo));
            userInfos.put(userInfo.getId(), userInfo);
        }
    }
}
